package zhangtao.bwie.com.yuekao2;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.Map;

import Bean.XiangBean;
import Presenter.IPresenter;
import Views.IView;

/**
 * Created by ZhangTAO on 2017/12/21.
 */

public class Xiang_act extends AppCompatActivity implements IView{

    private SimpleDraweeView icons;
    private TextView desc;
    private TextView price;
    private Button addcars;
    private IPresenter iPresenter;
    private XiangBean.DataBean xinagdata;
    private HashMap<String, String> map;
    private int pid1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xiangqing);
        Fresco.initialize(this);
        initView();
        final Intent intent = getIntent();
        pid1 = intent.getIntExtra("pid", 0);
        map = new HashMap<>();
        map.put("pid", pid1+"");
        setPresenter("https://www.zhaoapi.cn/",map);
        addcars.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(Xiang_act.this,Cars_act.class);
                startActivity(intent1);
                Toast.makeText(Xiang_act.this,"添加成功",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
    private void initView() {
        icons = (SimpleDraweeView) findViewById(R.id.shop_icons_xiangqing);
        desc = (TextView) findViewById(R.id.tit_desc_xiangqing);
        price = (TextView) findViewById(R.id.price_xiangqing);
        addcars = (Button) findViewById(R.id.add_cars);
    }
    public void setPresenter(String url, Map<String,String> map) {
        iPresenter = new IPresenter();
        iPresenter.attach(this);
        iPresenter.getData(url,map);
    }

    @Override
    public void onSuccess(Object o) {
        if(o instanceof XiangBean) {
            XiangBean bean = (XiangBean) o;
            if(bean != null) {
                xinagdata = bean.getData();
                Log.d("zzz",xinagdata.toString());
                String images = xinagdata.getImages();
                if(images.contains("|")) {
                    String[] split = images.split("[|]");
                    icons.setImageURI(split[1]);
                }else {
                    icons.setImageURI(images);
                }
                price.setText(xinagdata.getPrice()+"");
                desc.setText(xinagdata.getTitle());
            }
        }
    }

    @Override
    public void onFailed(Exception e) {

    }
}
